import { IColumns } from "../../ExcelStore";
import BrowserCellType from "./BrowserCellType";
import CheckBoxCellType, { CheckBoxType } from "./CheckBoxCellType";
import InputCellType from "./InputCellType";
import SelectCellType from "./SelectCellType";
import DateTimeCellType from "./DateTimeCellType";
export enum FieldTypeName {
    Text = 'text',
    Int = 'int',
    Float = 'float',
    Thousand = 'thousand',
    Money = 'money',
    Check = 'check',
    Browser = 'browser',
    DateTime = 'dateTime',
    Select = 'select',
    Radio = 'radio',
    Checkbox = 'checkbox',
    Upload = 'upload'
}

export interface IBrowserValue {
    hreflink: string;
    datas: { id: string, name: string, names: string, linkUrl: string }[]
}
export const getBrowserValue = (value: string) => {
    try {
        let result: IBrowserValue = JSON.parse(value);
        //if (isArray(result.datas)) {
        return result.datas;
        // }
    } catch (e) {
    }
    return [];
}
export const getInputValue = (value: string | object) => {
    try {
        if (typeof value === 'string') {
            const { value: v } = JSON.parse(value);
            //if (isArray(result.datas)) {
            return v;
            // }

        } else {
            return value['value'] || '';
        }
    } catch (e) {
        if (typeof value === 'string'){
            //value值中可能含有双引号,导致转换成json对象报错
            let start=value.indexOf("value");
            let end=value.lastIndexOf('"');
            if(end>(start+8)){
                return value.substring(start+8,end);
            }
        }
    }
    return "";
}
export const getHreflink = (value: string) => {
    try {
        const { hreflink } = JSON.parse(value);
        //if (isArray(result.datas)) {
        return hreflink;
        // }
    } catch (e) {
    }
    return "";
}
const dotPng = '';
export const dotPngImg = document.createElement('img');
dotPngImg.src = dotPng;

export const linkedColor = '#2db7f5';
const getEditorType = (col: IColumns) => {
    const { typeName } = col;
    if (typeName === FieldTypeName.Browser) {
        const Type = BrowserCellType(col, true);
        return new Type();
    } else if (typeName === FieldTypeName.DateTime) {
        const Type = DateTimeCellType(col, true);
        return new Type();
    } else if (typeName === FieldTypeName.Select) {
        const Type = SelectCellType(col, true);
        return new Type(col.options);
    } else if (typeName === FieldTypeName.Check) {
        const Type = CheckBoxCellType(col, true);
        return new Type();
    } else if (typeName === FieldTypeName.Checkbox) {
        const Type = CheckBoxCellType(col, true);
        const cellType = new Type(CheckBoxType.MutilCheck);
        cellType.items(col.options || []);
        return cellType;
    } else if (typeName === FieldTypeName.Radio) {
        const Type = CheckBoxCellType(col, true);
        const cellType = new Type(CheckBoxType.Raido);
        cellType.items(col.options || []);
        return cellType;
    } else if (typeName === FieldTypeName.Text || typeName === FieldTypeName.Int || typeName === FieldTypeName.Float || typeName === FieldTypeName.Thousand || typeName === FieldTypeName.Money) {
        const Type = InputCellType(col, true);
        return new Type();
    }
    const Default = InputCellType({
        ...col,
        typeName: FieldTypeName.Text
    }, false);
    return new Default();
}
export default (col: IColumns, isEditable = false): any => {
    const { typeName } = col;
    if (col.column === 'id' || col.column === 'modedatacreatedate' || col.column === 'modedatamodifydatetime') {
        const Type = InputCellType({ ...col, typeName: FieldTypeName.Text });
        return new Type();
    } else if (col.column === 'modedatacreater'|| col.column === 'modedatamodifier' ) {
        const Type = BrowserCellType({ ...col, type: '1', typeName: FieldTypeName.Browser, dbtype: 'int' }, false);
        return new Type();
    } else if (isEditable) {
        return getEditorType(col);
    }
    if (typeName === FieldTypeName.Browser) {
        const Type = BrowserCellType(col, false);
        return new Type();
    } else if (typeName === FieldTypeName.DateTime) {
        const Type = DateTimeCellType(col, false);
        return new Type();
    } else if (typeName === FieldTypeName.Select) {
        const Type = SelectCellType(col, false);
        return new Type(col.options || []);
    } else if (typeName === FieldTypeName.Text || typeName === FieldTypeName.Int || typeName === FieldTypeName.Float || typeName === FieldTypeName.Thousand || typeName === FieldTypeName.Money) {
        const Type = InputCellType(col);
        return new Type();
    } else if (typeName === FieldTypeName.Check) {
        const Type = CheckBoxCellType(col, false);
        return new Type();
    } else if (typeName === FieldTypeName.Checkbox) {
        const Type = CheckBoxCellType(col, false);
        const cellType = new Type(CheckBoxType.MutilCheck);
        cellType.items(col.options || []);
        return cellType;
    } else if (typeName === FieldTypeName.Radio) {
        const Type = CheckBoxCellType(col, false);
        const cellType = new Type(CheckBoxType.Raido);
        cellType.items(col.options || []);
        return cellType;
    }
    const Default = InputCellType({
        ...col,
        typeName: FieldTypeName.Text
    }, false);
    return new Default();
}